package com.dxl.lanju.dao.test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.dxl.common.utils.BeanUtils;
import com.dxl.common.utils.Utils;
import com.dxl.entity.account.Function;
import com.dxl.entity.common.BaseEntity;

public class EntityUtils {
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/account/Account.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/account/AccountRole.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/account/Function.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/account/Role.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/account/RoleFunction.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/config/SysProp.java
//	/cms-entitybean/src/main/java/com/dxl/cms/entity/log/OptLog.java
	private  Class<? extends BaseEntity> clazz = Function.class;
	
//	@Test
	public void beanToTableOracle() {
		StringBuilder sb = new StringBuilder("create table ");
		String tbName = Utils.toDBName(clazz.getSimpleName());
		sb.append(tbName);
		sb.append(" (");
		List<Field> fields = BeanUtils.listFields(clazz);
		int i = 0;
		Map<String, String> notesMap = new HashMap<String, String>();
		for (Field field : fields) {
			if(field.isAnnotationPresent(note.class)){
				notesMap.put(Utils.toDBName(field.getName()), field.getAnnotation(note.class).value());
			}
			if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
				continue;
			}
			String typeName = field.getType().getName();
			if (i > 0) {
				sb.append(", \n");
			}
			if (int.class.getName().equals(typeName) 
					|| Integer.class.getName().equals(typeName)
					|| long.class.getName().equals(typeName) 
					|| Long.class.getName().equals(typeName)
					|| short.class.getName().equals(typeName) 
					|| Short.class.getName().equals(typeName)
					|| byte.class.getName().equals(typeName) 
					|| Byte.class.getName().equals(typeName)
					|| boolean.class.getName().equals(typeName) 
					|| Boolean.class.getName().equals(typeName)) {
				sb.append(Utils.toDBName(field.getName())).append(" number(16)");
				i++;
			} else if (float.class.getName().equals(typeName) 
					|| Float.class.getName().equals(typeName)
					|| double.class.getName().equals(typeName) 
					|| Double.class.getName().equals(typeName)
					|| BigDecimal.class.getName().equals(typeName)) {
				sb.append(Utils.toDBName(field.getName())).append(" number(38,16)");
				i++;
			} else if (String.class.getName().equals(typeName)) {
				sb.append(Utils.toDBName(field.getName())).append(" varchar2(255)");
				i++;
			} else if (Date.class.getName().equals(typeName)) {
				sb.append(Utils.toDBName(field.getName())).append(" date");
				i++;
			}
		}
		sb.append(")\n");
		sb.append("tablespace BIGDATA\n");
		sb.append(" storage\n");
		sb.append("(\n");
		sb.append("initial 2M\n");
		sb.append("next 2M\n");
		sb.append("minextents 1\n");
		sb.append("maxextents unlimited\n");
		sb.append("pctincrease 0\n");
		sb.append(" );\n");
		sb.append("alter table ").append(tbName).append("\n");
		sb.append("add constraint ").append(tbName).append("_pk primary key (ID);\n");
		
		//添加注释
		if(clazz.isAnnotationPresent(note.class)){
			sb.append("comment on table " + tbName + " is '" + clazz.getAnnotation(note.class).value() + "';\n");
		}
		for(Map.Entry<String, String> entry : notesMap.entrySet()){
			sb.append("comment on column " + tbName + "." + entry.getKey() + " is '" + entry.getValue() + "';\n");
		}
		
		System.out.println(sb.toString());
	}
	
	@Test
	public void beanToTableMysql() {
		StringBuilder sb = new StringBuilder("create table ");
		String tbName = Utils.toDBName(clazz.getSimpleName());
		sb.append(tbName);
		sb.append(" (");
		List<Field> fields = BeanUtils.listFields(clazz);
		int i = 0;
		for (Field field : fields) {
			if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
				continue;
			}
			String typeName = field.getType().getName();
			if (i > 0) {
				sb.append(", \n");
			}
			if (int.class.getName().equals(typeName) 
					|| Integer.class.getName().equals(typeName)
					|| long.class.getName().equals(typeName) 
					|| Long.class.getName().equals(typeName)
					|| short.class.getName().equals(typeName) 
					|| Short.class.getName().equals(typeName)
					|| byte.class.getName().equals(typeName) 
					|| Byte.class.getName().equals(typeName)
					|| boolean.class.getName().equals(typeName) 
					|| Boolean.class.getName().equals(typeName)) {
				sb.append("").append(Utils.toDBName(field.getName())).append(" bigint(16) NULL");
				i++;
			} else if (float.class.getName().equals(typeName) 
					|| Float.class.getName().equals(typeName)
					|| double.class.getName().equals(typeName) 
					|| Double.class.getName().equals(typeName)
					|| BigDecimal.class.getName().equals(typeName)) {
				sb.append("").append(Utils.toDBName(field.getName())).append(" decimal(32,16) NULL");
				i++;
			} else if (String.class.getName().equals(typeName)) {
				
				sb.append("").append(Utils.toDBName(field.getName())).append(" varchar("+(field.getName().equals("id") ? "36" : "255")+") " + (field.getName().equals("id") ? "NOT " : "") + "NULL");
				i++;
			} else if (Date.class.getName().equals(typeName)) {
				sb.append("").append(Utils.toDBName(field.getName())).append(" datetime NULL");
				i++;
			}
		}
		sb.append(",PRIMARY KEY (`id`)) ENGINE=InnoDB;");
		System.out.println(sb.toString());
	}
}
